import unittest
from .testa import TestA
from .testb import TestB
from .HTMLTestRunner import HTMLTestRunner


class LoadCase(unittest.TestCase):

    def __init__(self):
        print(u"============初始化方法到底啥时候执行，执行几次？")

    def test_load_fun(self):
        suite = unittest.TestSuite()
        suite.addTest(TestA("test_a"))

        runner = unittest.TextTestRunner(verbosity=2)
        runner.run(suite)

    def test_load_funs(self):
        suite = unittest.TestSuite()

        tests = [TestA("test_a"),TestA("test_b"),TestB("test_a")]
        suite.addTests(tests)

        runner = unittest.TextTestRunner(verbosity=2)
        runner.run(suite)

    def test_load_name(self):
        suite = unittest.TestSuite()
        # 此处的路径需要从工程路径开始写根路径
        load_model_tuple = unittest.TestLoader().loadTestsFromName("test.testa.TestA")
        suite.addTests(load_model_tuple)

        runner = unittest.TextTestRunner(verbosity=2)
        runner.run(suite)

    def test_load_names(self):
        suite = unittest.TestSuite()
        # TestLoader+loadTestsFromNames
        load_model_tuple = unittest.TestLoader().loadTestsFromNames(["test.testa.TestA","test.testb.TestB"])
        suite.addTests(load_model_tuple)

        runner = unittest.TextTestRunner(verbosity=2)
        runner.run(suite)

    def test_load_case(self):
        suite = unittest.TestSuite()
        # TestLoader+loadTestsFromNames
        load_model_tuple = unittest.TestLoader().loadTestsFromTestCase(TestA)
        suite.addTests(load_model_tuple)

        runner = unittest.TextTestRunner(verbosity=2)
        runner.run(suite)

    def test_load_model(self):
        suite = unittest.TestSuite()
        # TestLoader+loadTestsFromNames
        load_model_tuple = unittest.TestLoader().loadTestsFromModule("test.testa")
        suite.addTests(load_model_tuple)

        runner = unittest.TextTestRunner(verbosity=2)
        runner.run(suite)

    def test_load_discover(self):
        """
        这个是最常用的方法，可以处理指定包下的目录
        :return:
        """
        discover = unittest.defaultTestLoader.discover(start_dir="test", pattern="*test*.py")
        runner = unittest.TextTestRunner(verbosity=2)
        runner.run(discover)

    def test_html_rpt(self):
        discover = unittest.defaultTestLoader.discover(start_dir="test", pattern="*test*.py")
        st = open("D:\\jtalk_report.html", "wb")
        # 引入扩展类
        runner = HTMLTestRunner(stream=st, verbosity=2, title=u"Jtalk客服工作台自动化报告", description=u"Python用例报告")
        result = runner.run(discover)
        st.close()



